#include "USB0_ReportHandler.h"
#include "USB0_InterruptServiceRoutine.h"
#include "USB0_Register.h"
#include "USB0_Descriptor.h"

//-----------------------------------------------------------------------------
// Descriptor Declarations
//-----------------------------------------------------------------------------

const device_descriptor code DEVICEDESC =
{
   18,                                 // bLength
   0x01,                               // bDescriptorType
   0x1001,                             // bcdUSB
//   0x0002,
   0x00,                               // bDeviceClass
   0x00,                               // bDeviceSubClass
   0x00,                               // bDeviceProtocol
   EP0_PACKET_SIZE,                    // bMaxPacketSize0
   0xC410,                             // idVendor
   0x0001,                             // idProduct
   0x0000,                             // bcdDevice
   0x01,                               // iManufacturer
   0x02,                               // iProduct
   0x00,                               // iSerialNumber
   0x01                                // bNumConfigurations
}; //end of DEVICEDESC

// From "USB Device Class Definition for Human Interface Devices (HID)".
// Section 7.1:
// "When a Get_Descriptor(Configuration) request is issued,
// it returns the Configuration descriptor, all Interface descriptors,
// all Endpoint descriptors, and the HID descriptor for each interface."
const hid_configuration_descriptor code HIDCONFIGDESC =
{

	{ // configuration_descriptor hid_configuration_descriptor
	   	0x09,                       	// Length
	   	0x02,                         	// Type
	   	0x2900,                      	// Totallength (= 9+9+9+7+7)
	   	0x01,                         	// NumInterfaces
	   	0x01,                          	// bConfigurationValue
	   	0x00,                          	// iConfiguration
	   	0x80,                          	// bmAttributes
	   	0x20                         	// MaxPower (in 2mA units)
	},

	{ // interface_descriptor hid_interface_descriptor
	   	0x09,                         	// bLength
	   	0x04,                       	// bDescriptorType
	   	0x00,                        	// bInterfaceNumber
	   	0x00,                        	// bAlternateSetting
	   	0x02,                        	// bNumEndpoints  !!!!!!!!!!!!!!!!!WAS 1
	   	0x03,                        	// bInterfaceClass (3 = HID)
	   	0x00,                         	// bInterfaceSubClass
	   	0x00,                        	// bInterfaceProcotol
	   	0x00                        	// iInterface
	},

	{ // class_descriptor hid_descriptor
		0x09,	                   		// bLength
		0x21,	                     	// bDescriptorType
		0x0101,	                     	// bcdHID
		0x00,	                    	// bCountryCode
		0x01,	                      	// bNumDescriptors
		0x22,                         	// bDescriptorType
		HID_REPORT_DESCRIPTOR_SIZE_LE  	// wItemLength (tot. len. of report
	                                 	// descriptor)
	},

	// IN endpoint (mandatory for HID)
	{ // endpoint_descriptor hid_endpoint_in_descriptor
	   	0x07,                         	// bLength
	   	0x05,                         	// bDescriptorType
	   	0x81,                        	// bEndpointAddress
	   	0x03,                          	// bmAttributes
	   	EP1_PACKET_SIZE_LE,           	// MaxPacketSize (LITTLE ENDIAN)
	   	10                            	// bInterval
	},

	// OUT endpoint (optional for HID)
	{ // endpoint_descriptor hid_endpoint_out_descriptor
	   	0x07,                         	// bLength
	   	0x05,                          	// bDescriptorType
	   	0x01,                          	// bEndpointAddress
	   	0x03,                         	// bmAttributes
	   	EP2_PACKET_SIZE_LE,            	// MaxPacketSize (LITTLE ENDIAN)
	   	10                           	// bInterval
	}
};


// Comment in with report information and value entered for 
// HID_REPORT_DESCRIPTOR_SIZE in F3xx_USB0_Descriptor.h

code const hid_report_descriptor HIDREPORTDESC =
{
   	0x06, 0x00, 0xFF,					// usage page (Vendor-defined page 1)
   	0x09, 0x01, 						// usage (Vendor-usage 1)
   	0xA1, 0x01,							// collection (application)

	0x85, 1,
	0x09, 1,							// usage (vendor spicific)
	0x15, 0,							// logical minimum (0)
 	0x26, 0xFF, 0x00,					// logical maximum (255)
   	0x95, 1,							// report count (# of bytes)
   	0x75, 8,							// report size  (# of bits)
	0x81, 2,							// input

   	0x85, 2,							// report ID
	0x09, 1,							// usage (vendor spicific)
	0x15, 0,							// logical minimum (0)
   	0x26, 0xFF, 0x00,					// logical maximum (255)
   	0x95, 1,							// report count (# of bytes)
   	0x75, 8,							// report size  (# of bits)
	0x91, 2,							// output

    0xC0								// end collection
};

#define STR0LEN 4

code unsigned char String0Desc [STR0LEN] =
{
   STR0LEN, 0x03, 0x09, 0x04
}; // End of String0Desc

#define STR1LEN sizeof ("SLAB") * 2

code unsigned char String1Desc [STR1LEN] =
{
   STR1LEN, 0x03,
   'S', 0,
   'L', 0,
   'A', 0,
   'B', 0
}; // End of String1Desc

#define STR2LEN sizeof ("F320") * 2

code unsigned char String2Desc [STR2LEN] =
{
   STR2LEN, 0x03,
   'F', 0,
   '3', 0,
   '2', 0,
   '0', 0
}; // End of String2Desc

unsigned char* const STRINGDESCTABLE [] =
{
   String0Desc,
   String1Desc,
   String2Desc
};